home *** CD-ROM | disk | FTP | other *** search
- Path: seas.smu.edu!not-for-mail
- From: dbowman@post.smu.edu (Damon Bowman)
- Newsgroups: comp.lang.c++
- Subject: Don't understand the Comb sort method
- Date: 13 Mar 1996 17:27:47 -0600
- Organization: Southern Methodist University
- Sender: usenet@seas.smu.edu
- Message-ID: <31475c41.460876069@sun.cis.smu.edu>
- Reply-To: dbowman@post.smu.edu
- NNTP-Posting-Host: sun.cis.smu.edu
- X-Nntp-Posting-Host: ax4-20.ppp.smu.edu
- X-Newsreader: Forte Agent .99d/32.182
-
-
- I am just teaching myself C++, and my book uses the Comb sort in the
- array section as an example of how to sort an array of integers.
-
- The Comb sort uses an offset value, which the book says is initialized
- to either 1 or to (8 * numberOfElements / 11), whichever is larger.
-
- Why is 8 * numberOfElements / 11 used?
-
- The strange part (to me) is that the only way 1 would be larger than
- (8 * n / 11) is if there were only 1 element, in which case the array
- would obviously not need sorting.
-
- Here's the source code from the example, which I have compiled and
- run. It works as advertised.
-
- //C++ program that sorts arrays using the Comb Sort method
-
- #include <iostream.h>
-
- const int MAX = 10;
- const int TRUE = 1;
- const int FALSE = 0;
-
- int obtainNumData()
- {
- int m;
- do
- {
- cout << "Enter number of data points [2 to " << MAX << "] : ";
- cin >> m;
- cout << "\n";
- }
- while (m < 2 || m > MAX);
- return m;
- }
-
- void inputArray(int intArr[], int n)
- {
- for (int i = 0; i < n; i++)
- {
- cout << "arr[" << i << "] : ";
- cin >> intArr[i];
- }
- }
-
- void showArray(int intArr[], int n)
- {
- for (int i = 0; i < n; i++)
- {
- cout.width(5);
- cout << intArr[i] << " ";
- }
- cout << "\n";
- }
-
- void sortArray(int intArr[], int n)
- {
- int offset, temp, inOrder;
- offset = n;
- do
- {
- offset = (8 * offset) / 11;
- offset = (offset == 0) ? 1 : offset;
- inOrder = TRUE;
- for (int i = 0, j = offset; i < (n-offset); i++, j++)
- {
- if (intArr[i] > intArr[j])
- {
- inOrder = FALSE;
- temp = intArr[i];
- intArr[i] = intArr[j];
- intArr[j] = temp;
- }
- }
- }
- while (!(offset = 1 && inOrder == TRUE));
- }
-
- main()
- {
- int arr[MAX];
- int n;
-
- n = obtainNumData();
- inputArray(arr, n);
- cout << "Unordered array is:\n";
- showArray(arr, n);
- sortArray(arr, n);
- cout << "\nSorted Array is:\n";
- showArray(arr, n);
- return 0;
- }
-